﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace _3dtextxna
{
    public static class SpriteFontHelper
    {
        private static GraphicsDevice GraphicsDevice;
        private static SpriteBatch Spritbatch;

        public static void Initialize(GraphicsDevice graphicsDevice)
        {
            GraphicsDevice = graphicsDevice;
            Spritbatch = new SpriteBatch(graphicsDevice);
        }

        public static Texture2D SpriteFontTextToTexture(SpriteFont font, string text)
        {
            Vector2 size = font.MeasureString(text);
            RenderTarget2D renderTarget = new RenderTarget2D(GraphicsDevice, (int)size.X, (int) size.Y);
            GraphicsDevice.SetRenderTarget(renderTarget);

            GraphicsDevice.Clear(Color.Transparent);

            Spritbatch.Begin();
            Spritbatch.DrawString(font, text, Vector2.Zero, Color.White);
            Spritbatch.End();

            GraphicsDevice.SetRenderTarget(null);

            return renderTarget;
        }

        public static void DrawString3D(this SpriteBatch spriteBatch, SpriteFont font, string text, Vector3 source, Matrix projection, Matrix view, Matrix world)
        {
            Vector3 position3d = GraphicsDevice.Viewport.Project(source, projection, view, world);
            Vector2 position2d = new Vector2(position3d.X, position3d.Y);

            spriteBatch.DrawString(font, text, position2d, Color.White);
        }
    }
}
